//
//  XYMorePriceView.m
//  siwe
//
//  Created by 肖兰月 on 2019/1/9.
//  Copyright © 2019 siwe. All rights reserved.
//

#import "LLClusterListPopView.h"
#import "LLMallDoModel.h"
#import "LLMallDoCell.h"

#define LLRowHeight 60

@interface LLClusterListPopView ()<UITableViewDelegate, UITableViewDataSource, LLMallDoCellDelegate>
@property (strong, nonatomic) UIButton *bgCover;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UILabel *titleLbl;
@property (strong, nonatomic) LLButton *closeBtn;
@property (strong, nonatomic) CALayer *separateLine;
@end

@implementation LLClusterListPopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    _bgCover = [UIButton new];
    _bgCover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _bgCover.frame = self.bounds;
    [self addSubview:_bgCover];
    
    _contentView = UIView.new;
    _contentView.backgroundColor = UIColor.whiteColor;
    _contentView.layer.cornerRadius = 7;
    _contentView.clipsToBounds = YES;
    [self addSubview:_contentView];
    
    _closeBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"close" titleColor:nil font:0];
    _closeBtn.backgroundColor = LLColorRGB(160);
    _closeBtn.layer.cornerRadius = 15;
    _closeBtn.size = CGSizeMake(30, 30);
    [_closeBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeBtn];
    
    _titleLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:18 textAlignment:NSTextAlignmentCenter];
    [_contentView addSubview:_titleLbl];
    
    _separateLine = CALayer.layer;
    _separateLine.backgroundColor = LLColorRGB(238).CGColor;
    [_contentView.layer addSublayer:_separateLine];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    _tableView.backgroundColor = LLColorRGB(255);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerCellByClassName:@"LLMallDoCell"];
    _tableView.rowHeight = LLRowHeight;
    _tableView.tableFooterView = UIView.new;
//    _tableView.layer.cornerRadius = 8;
    _tableView.separatorInset = UIEdgeInsetsZero;
    [_contentView addSubview:_tableView];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLbl.text = title;
}

- (void)setModels:(NSArray *)models {
    _models = models;
    [self layoutUI];
    [self.tableView reloadData];
}

- (void)layoutUI {
    CGFloat tableViewH = 0;
    if (self.models.count < 6) {
        tableViewH = self.models.count*LLRowHeight;
    } else {
        tableViewH = 5*LLRowHeight + 20;
    }
    CGFloat contentViewLeft = LLScreenWidth < 375 ? 30 : 40;
    CGFloat contentViewW = self.width-contentViewLeft*2;
    self.contentView.frame = CGRectMake(contentViewLeft, (self.height-tableViewH-64)/2.0, contentViewW, tableViewH+64);
    self.closeBtn.origin = CGPointMake(self.contentView.right-15, self.contentView.top-15);
    self.titleLbl.frame = CGRectMake(0, 0, contentViewW, 63);
    self.separateLine.frame = CGRectMake(0, self.titleLbl.bottom, contentViewW, 1);
    self.tableView.frame = CGRectMake(0, self.separateLine.bottom, contentViewW, tableViewH);
}

- (void)show {
    self.hidden = NO;
}

- (void)hide {
    self.hidden = YES;
}

#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LLMallDoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMallDoCell"];
    cell.type = LLMallDoCellTypeMore;
    cell.delegate = self;
    cell.model = self.models[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - LLMallDoCellDelegate

- (void)cellDidClickDo:(LLMallDoCell *)cell {
    if ([self.delegate respondsToSelector:@selector(clusterListPopViewDidClickCell:)]) {
        [self.delegate clusterListPopViewDidClickCell:cell];
    }
}


@end
